<?php
namespace jy_kexie;
defined('BY_JYA') or exit('error');
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_cmso2o/model')->方法名();
*/
class model extends \plugin{
	public function get($params,&$total=null,$lat_lng=array()){
		if(!$params['orderby']){
			$params['orderby']=array('id desc');
		}
		$list=$this->pdo_getlist('store',$params,$total);
		foreach($list as &$v){
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
			$v['juli']=intval($v['juli']/1000);
		}
		paixu($return['list'],'juli','asc');
		return $list;
	}
	public function checkuser($mid=''){
		$user=pdo_get('jy_kexie_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$user['username']){
			exi('请先绑定学员账号密码','error','/set/');
		}
		/*if(empty($user) && $_SESSION['uid']){
			$data=array(
				'mid'=>$_SESSION['uid'],
				'createtime'=>time(),
				'uniacid'=>$_SESSION['uniacid']
			);
			pdo_insert('jy_kexie_user',$data);
		}*/
		return $user;
	}
	public function add_sign($params=array()){
		$user=pdo_get('jy_kexie_sign',array('mid'=>$_SESSION['uid'],'lesson_paiid'=>$params['lesson_paiid'],'uniacid'=>$_SESSION['uniacid']));
		if(empty($user) && $_SESSION['uid']){
			$data=array(
				'lesson_paiid'=>$params['lesson_paiid'],
				'storeid'=>$params['storeid'],
				'type'=>$params['type'],
				'mid'=>$_SESSION['uid'],
				'createtime'=>time(),
				'uniacid'=>$_SESSION['uniacid']
			);
			pdo_insert('jy_kexie_sign',$data);
		}
		return true;
	}
	public function msgtpl_tags($area){
		$tags=array(
			array('title'=>'通用类','data'=>array(
				array('name'=>'平台名称'),
				array('name'=>'粉丝昵称'),
			)),
			array('title'=>'订单类','data'=>array(
				array('name' => '平台名称'),
				array('name' => '粉丝昵称'),
				array('name' => '订单号'),
				array('name' => '订单金额'),
				array('name' => '课程名称'),
				array('name' => '课程老师'),
				array('name' => '班级名称'),
				array('name' => '班主任'),
				array('name' => '监护人'),
				array('name' => '监护人电话'),
				array('name' => '学员姓名'),
				array('name' => '下单时间'),
				array('name' => '支付时间'),
				array('name' => '取消时间'),
				array('name' => '门店'),
				array('name' => '门店地址'),
				array('name' => '门店联系人'),
				array('name' => '门店营业时间'),
				array('name' => '学号'),
				array('name' => '备注信息'),
				array('name' => '当前时间'),
		))
		);
		return $tags;
	}
	//导出excel
	public function export2excel($excel,$filename,$headerkey=false,$save_filename=''){
		$html="\xEF\xBB\xBF";
		foreach($excel as $ev){
			$header=$ev['header'];
			$data=$ev['data'];
			if($headerkey){
				foreach ($header as $k=>$li) {
					$html .= $k . "\t ,";
				}
				$html.="\n";
			}
			foreach ($header as $li) {
				$html .= $li . "\t ,";
			}
			$html.="\n";
			$keys=array_keys($header);
			foreach($data as $row){
				foreach($keys as $key){
					$cont=str_replace(',','|jy|',$row[$key]);
					$html.=$cont."\t,";
				}
				$html.="\n";
			}
		}
		if($save_filename){
			$filename=iconv("UTF-8","gbk",$filename);
			$save_filename=$save_filename.'/'.$filename.'.csv';
			model('core/files')->source_save($html,'','csv',$filename,$save_filename,'','',true);
			return true;
		}
		ob_clean();
		header("Content-type:text/csv");
		header("Content-Disposition:attachment; filename=".$filename.".csv");
		echo $html;
		exit();
	}
}